package com.example.musicplayer;

import java.util.List;

import com.example.musicplayer.bean.Music;
import com.example.musicplayer.common.MusicConstant;
import com.example.musicplayer.service.MusicPlaybackUtils;
import com.example.musicplayer.service.MusicService;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MusicPlaybackActivity extends Activity implements OnClickListener {

    private TextView mTextView;
    private Button mPlayBtn;
    private Button mStopBtn;
    private Button mPrevBtn;
    private Button mNextBtn;

    private Uri mMusicUri;
    private String mSongName;

    private List<Music> mMusics;
    private int mCurrentPosition = 0;
    private Music mCurrentMusic = null;

    private MusicPlaybackUtils mPlaybackUtils;

    private static final int BIND_MUSIC_SERVICE_DELAY_TIME = 1000;

    private static final int MSG_START_MUSIC_SERVICE = 1;
    private static final int MSG_BIND_MUSIC_SERVICE = MSG_START_MUSIC_SERVICE + 1;
    private static final int MSG_START_PLAY = MSG_BIND_MUSIC_SERVICE + 1;
    private static final int MSG_PAUSE_PLAY = MSG_START_PLAY + 1;
    private static final int MSG_STOP_PLAY = MSG_PAUSE_PLAY + 1;
    private static final int MSG_PLAY_PREV = MSG_STOP_PLAY + 1;
    private static final int MSG_PLAY_NEXT = MSG_PLAY_PREV + 1;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_BIND_MUSIC_SERVICE:
                bindMusicService();
                break;
            case MSG_START_PLAY:
                mPlaybackUtils.play();
                break;
            case MSG_PAUSE_PLAY:
                mPlaybackUtils.pause();
                break;
            case MSG_STOP_PLAY:
                mPlaybackUtils.stop();
                break;
            case MSG_PLAY_PREV:
                mPlaybackUtils.prev();
                break;
            case MSG_PLAY_NEXT:
                mPlaybackUtils.next();
                break;
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_music_play);

        initViews();
        initData();
        initEvent();

        mPlaybackUtils = MusicPlaybackUtils.getInstance(getApplicationContext());
        startMusicService();

        mHandler.sendEmptyMessageDelayed(MSG_BIND_MUSIC_SERVICE, BIND_MUSIC_SERVICE_DELAY_TIME);
    }

    private void initViews() {
        mTextView = (TextView) findViewById(R.id.txt);

        mPlayBtn = (Button) findViewById(R.id.play);
        mStopBtn = (Button) findViewById(R.id.stop);
        mPrevBtn = (Button) findViewById(R.id.prev);
        mNextBtn = (Button) findViewById(R.id.next);

        mPlayBtn.setText(R.string.pause);
    }

    private void initData() {
        if (getIntent() != null) {
            Intent intent = getIntent();
            mMusicUri = intent.getData();
            if (intent.hasExtra(MusicConstant.EXTRA_SONG_NAME)) {
                mSongName = intent.getStringExtra(MusicConstant.EXTRA_SONG_NAME);
            }
            if (intent.hasExtra(MusicConstant.EXTRA_SONG_LIST)) {
                mMusics = intent.getParcelableArrayListExtra(MusicConstant.EXTRA_SONG_LIST);
            }
            mCurrentPosition = intent.getIntExtra(MusicConstant.EXTRA_SONG_POSITION, 0);
            if (mMusics != null) {
                mCurrentMusic = mMusics.get(mCurrentPosition);
                mMusicUri = Uri.withAppendedPath(MusicConstant.MEDIA_EXTERNAL_CONTENT_URI,
                        String.valueOf(mCurrentMusic.getId()));

                mSongName = mCurrentMusic.getTitle();
            }
        }
    }

    private void initEvent() {
        mPlayBtn.setOnClickListener(this);
        mStopBtn.setOnClickListener(this);
        mPrevBtn.setOnClickListener(this);
        mNextBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.play:
            if (mPlaybackUtils.isPlaying()) {
                mHandler.sendEmptyMessage(MSG_PAUSE_PLAY);
                mPlayBtn.setText(R.string.play);
            } else {
                mHandler.sendEmptyMessage(MSG_START_PLAY);
                mPlayBtn.setText(R.string.pause);
            }
            break;
        case R.id.stop:
            mHandler.sendEmptyMessage(MSG_STOP_PLAY);
            mPlayBtn.setText(R.string.play);
            break;
        case R.id.prev:
            mHandler.sendEmptyMessage(MSG_PLAY_PREV);
            break;
        case R.id.next:
            mHandler.sendEmptyMessage(MSG_PLAY_NEXT);
            break;
        }
    }

    private void startMusicService() {
        Intent intent = new Intent(getApplicationContext(), MusicService.class);
        intent.setAction(MusicService.ACTION_PLAY);
        if (mMusicUri != null) {
            intent.setData(mMusicUri);
        }

        mPlaybackUtils.startService(intent);
    }

    private void bindMusicService() {
        Intent intent = new Intent(getApplicationContext(), MusicService.class);

        mPlaybackUtils.bindService(intent, Service.BIND_AUTO_CREATE);
    }
}
